package com.example.shoppingcenter.Ai.XF;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.WebSocket;

import java.util.List;
import java.util.UUID;

public class MyThread  extends Thread {
    private BigModel bigModel;
    private WebSocket webSocket;

    public BigModel getBigModel() {
        return bigModel;
    }

    public WebSocket getWebSocket() {
        return webSocket;
    }

    public void setWebSocket(WebSocket webSocket) {
        this.webSocket = webSocket;
    }

    public void setBigModel(BigModel bigModel) {
        this.bigModel = bigModel;
    }

    public MyThread(BigModel bigModel) {
        this.bigModel = bigModel;
    }
    
    public MyThread(WebSocket webSocket) {
        this.webSocket = webSocket;
    }

    public MyThread(BigModel bigModel, WebSocket webSocket) {
        this.bigModel = bigModel;
        this.webSocket = webSocket;
    }

    @Override
    public synchronized void run() {
        List<RoleContent> historyList = bigModel.historyList;
        String NewQuestion = bigModel.getNewQuestion();
        boolean wsCloseFlag = bigModel.getWsCloseFlag();
        try {
            JSONObject requestJson=new JSONObject();

            JSONObject header=new JSONObject();  // header参数
            header.put("app_id",bigModel.getXf().getAppId());
            header.put("uid", UUID.randomUUID().toString().substring(0, 10));

            JSONObject parameter=new JSONObject(); // parameter参数
            JSONObject chat=new JSONObject();
            chat.put("domain","general");
            chat.put("temperature",0.5);
            chat.put("max_tokens",4096);
            parameter.put("chat",chat);

            JSONObject payload=new JSONObject(); // payload参数
            JSONObject message=new JSONObject();
            JSONArray text=new JSONArray();

            // 历史问题获取
            if(historyList.size()>0){
                for(RoleContent tempRoleContent:historyList){
                    text.add(JSON.toJSON(tempRoleContent));
                }
            }

            // 最新问题
            RoleContent roleContent=new RoleContent();
            roleContent.role="user";
            roleContent.content=NewQuestion;
            text.add(JSON.toJSON(roleContent));
            historyList.add(roleContent);


            message.put("text",text);
            payload.put("message",message);


            requestJson.put("header",header);
            requestJson.put("parameter",parameter);
            requestJson.put("payload",payload);
            // System.err.println(requestJson); // 可以打印看每次的传参明细
            webSocket.send(requestJson.toString());
            // 等待服务端返回完毕后关闭
            while (true) {
                // System.err.println(wsCloseFlag + "---");
                Thread.sleep(200);
                if (wsCloseFlag) {
                    break;
                }
            }
            webSocket.close(1000, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
